package com.boot.webflux.security.example2.handler;

import com.alibaba.fastjson.JSON;
import com.boot.webflux.security.example2.utils.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录成功处理类
 */
@Component
@Slf4j
public class CloudAuthenticationSuccessHandler  implements ServerAuthenticationSuccessHandler {
//    private JwtTokenUtil jwtTokenUtil;
    @Override
    public Mono<Void> onAuthenticationSuccess(WebFilterExchange exchange, Authentication authentication)  {
        log.info("-------SuccessHandler-----------"+ JSON.toJSONString(authentication));
        ServerHttpResponse response = exchange.getExchange().getResponse();
//        // 组装JWT
//        SysUser sysUser =  (SysUser) authentication.getPrincipal();
//        String token = jwtTokenUtil.generateToken(sysUser);
        // 封装返回参数
        Map<String,Object> resultData = new HashMap<>();
        resultData.put("code","200");
        resultData.put("msg", "登录成功");
        resultData.put("token","token-asd");
        return RespUtil.writeMsg(response,200,resultData);
    }
}
